setup Rspec Ruby On Rails API Mode bagian 1

RSpec adalah kerangka kerja pengujian unit untuk bahasa pemrograman Ruby. RSpec berbeda dari kerangka xUnit tradisional seperti JUnit karena RSpec adalah alat pengembangan yang digerakkan oleh Perilaku. Artinya, tes yang ditulis dalam RSpec berfokus pada “perilaku” aplikasi yang sedang diuji.

RSpec adalah DSL untuk membuat contoh yang dapat dieksekusi tentang bagaimana kode diharapkan berperilaku, terorganisir dalam kelompok. Itu menggunakan kata-kata “describe” dan “it” sehingga kita bisa mengungkapkan konsep seperti percakapan:

"Describe an account when it is first opened."
"It has a balance of zero."

1. setup dan install ror and rspec

Buat aplikasi baru dengan command berikut 

rails _7.0.1_ new sample_unit_test --api -d postgresql

pindah ke directory yang kita buat

 cd sample_unit_test 

buka file Gemfile tambahkan gem ‘rspec-rails’

kembali ke terminal dan run 

 bundle install 

create database dengan run 

 bundle exec rake db:create 

setup rspec ke rails dengan command berikut

bundle exec rails generate rspec:install  

2. membuat sebuah model customer dan fitur crud  yang akan kita test fiturnya

 bundle exec rails g scaffold customer name:string phone:bigint email:string address:text
bundle exec rake db:migrate

edit app/models/customer.rb tambahkan validasi

 class Customer < ApplicationRecord  
  validates :name, :email, presence: true  
 end  

3. update rspec file 
pada saat kita melakukan run  command scaffold  ror akan otomatis membuatkan file testnya juga

Gambar Hasil generate scaffold

untuk melakukan test pada controller customer edit file di spec/requests/customers_spec.rb

isi nya sebagian sudah ada di generate by system oleh ror tapi belum sempurna ada beberapa perbaikan pada params dan assertions kita harus pelajari code yang sudah ada di mulai dari index show create update dan delete dan juga harus paham dengan assertions dan matchers
isi default dari file test request customers

 Karena isinya masih kosong kita harus mengisikan attribute yang benar dan attribute yang salah gunakan gem “faker” untuk membantu membuat data sesuai

let(:valid_attributes) do  
   { name: Faker::Name.name,  
    phone: Faker::Number.number(digits: 12),  
    email: Faker::Internet.email,  
    address: Faker::Address.street_address }  
  end  
   
  let(:invalid_attributes) do  
   { name: nil,  
    phone: nil,  
    email: Faker::Internet.email,  
    address: Faker::Address.street_address }  
  end  
   
  let(:valid_headers) do  
   {}  
  end 

valid headers dikosongkan karena di tutorial ini tidak memakai security token 

setelah menambahkan valid_attributes dan invalid_attributes coba test dengan comand berikut

bundle exec rspec spec/requests/customers_spec.rb 
Gambar setelah update valid dan invalid attribute

pada keterangan errornya ada dua proses yang pending itu dikarenakan ada skip waktu bagian update
buka kembali file berikut

spec/requests/customers_spec.rb 

Gambar default code part update

bagian skip harus diganti sesuai keterangannya untuk new atrribute maksudnya isi data new_attributes untuk menggantikan data lama isinya seperti code dibawah

    let(:new_name) { Faker::Name.name }  
    let(:new_attributes) do  
     { name: new_name,  
      phone: Faker::Number.number(digits: 12),  
      email: Faker::Internet.email,  
      address: Faker::Address.street_address }  
    end 
untuk bagian “assertions” kita isi dengan pengecekan data nama setelah update sama dengan nama pada new_attributes

    it 'updates the requested customer' do  
     customer = Customer.create! valid_attributes  
     patch customer_url(customer),  
        params: { customer: new_attributes }, headers: valid_headers, as: :json  
     customer.reload  
     expect(customer.name).to eq(new_name)  
    end 
check kembali file test dengan command berikut

bundle exec rspec spec/requests/customers_spec.rb 
Gambar hasil pengujian sukses tanpa gagal

Test model customer update spec/models/customer_spec.rb seperti berikut  kemudian run testing

 require 'rails_helper'  
 RSpec.describe Customer, type: :model do  
  it 'is not valid without a name' do  
   customer = Customer.new(name: nil)  
   expect(customer).to_not be_valid  
  end  
  it 'is not valid without a email' do  
   customer = Customer.new(email: nil)  
   expect(customer).to_not be_valid  
  end  
 end 

testing untuk model customer disini cuma validasi sebenarnya bisa banyak tergantung isi dari modelnya

PakarPBN

A Private Blog Network (PBN) is a collection of websites that are controlled by a single individual or organization and used primarily to build backlinks to a “money site” in order to influence its ranking in search engines such as Google. The core idea behind a PBN is based on the importance of backlinks in Google’s ranking algorithm. Since Google views backlinks as signals of authority and trust, some website owners attempt to artificially create these signals through a controlled network of sites.

In a typical PBN setup, the owner acquires expired or aged domains that already have existing authority, backlinks, and history. These domains are rebuilt with new content and hosted separately, often using different IP addresses, hosting providers, themes, and ownership details to make them appear unrelated. Within the content published on these sites, links are strategically placed that point to the main website the owner wants to rank higher. By doing this, the owner attempts to pass link equity (also known as “link juice”) from the PBN sites to the target website.

The purpose of a PBN is to give the impression that the target website is naturally earning links from multiple independent sources. If done effectively, this can temporarily improve keyword rankings, increase organic visibility, and drive more traffic from search results.

Jasa Backlink

Download Anime Batch